package com.yunc.user.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.common.core.result.R;
import com.yunc.user.sys.entity.SysLog;
import com.yunc.user.sys.entity.dto.SysLogDTO;
import com.yunc.user.sys.service.ISysLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 日志
 *
 * @author congcong tao
 * @create 2018/11/18 上午10:21
 */
@RestController
@RequestMapping("sysLog")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysLogController {

    private final ISysLogService sysLogService;

    @GetMapping("/page")
    public R page(Page<SysLog> page, SysLog sysLog) {
        List<SysLog> sysLogList = sysLogService.pageInfo(page, sysLog);
        return R.ok().put("data", sysLogList).put("count", page.getTotal());
    }

    @DeleteMapping("/delete/{ids}")
    public R info(@PathVariable String ids) {
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = sysLogService.removeByIds(idsList);
        if (flag) {
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

    @GetMapping("/count")
    public R count(SysLog sysLog) {
        // 统计模块使用情况
        List<SysLogDTO> list = sysLogService.listEntity(sysLog);
        return R.ok().put("sysLogList", list);
    }

    @GetMapping("/countDate")
    public R countDate(SysLog sysLog) {
        // 按天统计,查询7天内统计情况
        List<SysLogDTO> list = sysLogService.listEntityDate(sysLog);
        return R.ok().put("sysLogList", list);
    }
}
